﻿using PRO.Permission.Entity;
using PRO.PermissionProvider;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace PRO.Permission
{
    public class PermissionLib
    {
        public static int AddPermissionGroup(PermissionGroup g)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().AddPermissionGroup(g);
            else
                return -1;
        }
        public static bool EditPermissionGroup(PermissionGroup g)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().EditPermissionGroup(g);
            else
                return false;
        }
        public static bool DelPermissionGroup(int id)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().DelPermissionGroup(id);
            else
                return false;
        }


        public static int AddPermission(PermissionItem g)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().AddPermission(g);
            else
                return -1;
        }
        public static bool EditPermission(PermissionItem g)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().EditPermission(g);
            else
                return false;
        }
        public static bool DelPermission(int id)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().DelPermission(id);
            else
                return false;
        }

        /// <summary>
        /// 获取所有权限信息
        /// </summary>
        /// <returns></returns>
        public static DataTable GetPermissions()
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().GetPermissions();
            else
                return null;
        }
        /// <summary>
        /// 获取指定分組所有权限信息
        /// </summary>
        /// <returns></returns>
        public static DataTable GetPermissionsByGroup(int groupid)
        {
            if(CheckPermissionDataBase())
                return BasePermission.GetInstance().GetPermissionsByGroup(groupid);
            else
                return null;
        }
        /// <summary>
        /// 获取泛型列表
        /// </summary>
        /// <param name="groupid">该参数大于0,获取指定分组的权限信息,否则获取全部</param>
        /// <returns></returns>
        public static List<PermissionItem> GetPermissionItemList(int groupid)
        {

            DataTable dt = groupid > 0 ? GetPermissionsByGroup(groupid) : GetPermissions();
            if(dt != null && dt.Rows.Count > 0)
            {
                List<PermissionItem> list = new List<PermissionItem>();
                for(int i = 0;i < dt.Rows.Count;i++)
                {
                    PermissionItem pi = GetPermissionItemByDr(dt.Rows[i]);
                    if(pi != null)
                    {
                        list.Add(pi);
                    }
                }
                return list;
            }
            return null;
        }


        public static DataTable GetPermissionGroup()
        {
            return BasePermission.GetInstance().GetPermissionGroup();
        }

        public static  List<PermissionGroup> GetPermissionGroupList()
        {
            DataTable dt = GetPermissionGroup();
            if(dt!=null&&dt.Rows.Count>0)
            {
                List<PermissionGroup> list = new List<PermissionGroup>();
                for(int i = 0;i < dt.Rows.Count;i++)
                {
                    PermissionGroup g = GetPermissionGroupByDr(dt.Rows[i]);
                    if(g!=null)
                    {
                        list.Add(g);
                    }
                }
                return list;
            }

            return null;
        }

        public static PermissionItem GetPermissionItemByDr(DataRow dr)
        {
            if(dr != null)
            {
                PermissionItem model = new PermissionItem();
                if(dr["Id"] != null && dr["Id"].ToString() != "")
                {
                    model.ID = int.Parse(dr["Id"].ToString());
                }
                if(dr["Permission"] != null && dr["Permission"].ToString() != "")
                {
                    model.Permission = dr["Permission"].ToString();
                }
                if(dr["GroupID"] != null && dr["GroupID"].ToString() != "")
                {
                    model.GroupID = int.Parse(dr["GroupID"].ToString());
                }
                if(dr["PermissionNote"] != null && dr["PermissionNote"].ToString() != "")
                {
                    model.PermissionNote = dr["PermissionNote"].ToString();
                }
                return model;
            }
            return null;

        }

        public static PermissionGroup GetPermissionGroupByDr(DataRow dr)
        {
            if(dr != null)
            {
                PermissionGroup model = new PermissionGroup();
                if(dr["id"] != null && dr["id"].ToString() != "")
                {
                    model.ID = int.Parse(dr["id"].ToString());
                }
                if(dr["GroupTitle"] != null && dr["GroupTitle"].ToString() != "")
                {
                    model.GroupTitle = dr["GroupTitle"].ToString();
                }
                if(dr["GroupNote"] != null && dr["GroupNote"].ToString() != "")
                {
                    model.GroupNote = dr["GroupNote"].ToString();
                }
                model.PermissionItems = GetPermissionItemList(model.ID);
            }
            return null;

        }
        /// <summary>
        /// 设置用户权限
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="permission">权限对应id组成的数组</param>
        /// <returns></returns>
        public static bool SetPermission(int userid, int[] permission)
        {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < permission.Length; i++)
            {
                if (!string.IsNullOrWhiteSpace(str.ToString()))
                    str.AppendFormat(",{0}", permission[i]);
                else
                    str.AppendFormat("{0}", permission[i]);
            }
            return BasePermission.GetInstance().SetPermission(userid, str.ToString());
        }

        private static bool PermissionDataBase_IsExist = false;
        /// <summary>
        /// 检查权限管理相关数据是否存在，不存在将自动建立
        /// </summary>
        /// <returns></returns>
        public static bool CheckPermissionDataBase()
        {
            bool r = false;
            if(PermissionDataBase_IsExist)
                r = true;
            else
                if (BasePermission.GetInstance().CheckPermissionDataBase())
                {
                    r = true;
                    PermissionDataBase_IsExist = r;
                }
            return r;
        }
    }
    public static class Permission
    {
        /// <summary>
        /// 验证用户权限
        /// </summary>
        /// <param name="userinfo"></param>
        /// <param name="permission">权限名称</param>
        /// <returns></returns>
        public static bool IsAuthorized(this BaseUserInfo userinfo,string permission)
        {
            bool r = false;
            if(!string.IsNullOrWhiteSpace(permission))
            {
                if(userinfo.ID > 0)
                {
                    if (!userinfo.Locked)
                    {
                        if (userinfo.GroupID == 0)//超级管理员
                            r = true;
                        else
                        {
                            //if(PermissionLib.CheckPermissionDataBase())
                            r = BasePermission.GetInstance().IsAuthorized(userinfo.ID, permission);
                        }
                    }
                }
            }
            return r;
        }

    }

}
